package org.example.kidscode.controller;

import org.example.kidscode.entity.ClassCard;
import org.example.kidscode.service.ClassCardService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/class-cards")
public class ClassCardController {

    private final ClassCardService classCardService;

    public ClassCardController(ClassCardService classCardService) {
        this.classCardService = classCardService;
    }

    @GetMapping
    public ResponseEntity<List<ClassCard>> getAllClassCards() {
        return ResponseEntity.ok(classCardService.findAll());
    }

    @GetMapping("/{cardId}")
    public ResponseEntity<ClassCard> getClassCardById(
            @PathVariable String cardId,
            HttpServletRequest request) throws Exception {

        String decodedCardId = URLDecoder.decode(cardId, StandardCharsets.UTF_8);
        return classCardService.findById(decodedCardId)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<ClassCard>> getClassCardsByStudentId(
            @PathVariable String studentId) {

        List<ClassCard> cards = classCardService.findByStudentId(studentId);
        return cards.isEmpty() ?
                ResponseEntity.noContent().build() :
                ResponseEntity.ok(cards);
    }

    @GetMapping("/expiring")
    public ResponseEntity<List<ClassCard>> getExpiringClassCards(
            @RequestParam(required = false)
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
            LocalDate date) {

        LocalDate queryDate = (date != null) ? date : LocalDate.now().plusDays(30);
        return ResponseEntity.ok(classCardService.findByExpireDateBefore(queryDate));
    }

    @PostMapping
    public ResponseEntity<ClassCard> createOrUpdateClassCard(
            @RequestBody ClassCard classCard) {

        return ResponseEntity.ok(classCardService.save(classCard));
    }

    @DeleteMapping("/{cardId}")
    public ResponseEntity<Void> deleteClassCard(@PathVariable String cardId) {
        return classCardService.findById(cardId)
                .map(card -> {
                    classCardService.deleteById(cardId);
                    return ResponseEntity.noContent().<Void>build();
                })
                .orElse(ResponseEntity.notFound().build());
    }
    @PutMapping("/{cardId}")
    public ResponseEntity<ClassCard> updateClassCard(
            @PathVariable String cardId,
            @RequestBody ClassCard classCard) {
        // 确保主键一致
        classCard.setCardId(cardId);
        return ResponseEntity.ok(classCardService.save(classCard));
    }
}